﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace GenericStorage
{
    public static class TypesResolve
    {
        //public static Dictionary<string, Guid> DBTypeName2Guid()
        //{
        //    Dictionary<string, Guid> types = new Dictionary<string, Guid>();
        //    using (SqlConnection con = new SqlConnection(DAL.connectionString))
        //    {
        //        con.Open();
        //        SqlCommand cmdSelect = new SqlCommand("SELECT * FROM [Types]", con);
        //        SqlDataReader reader = cmdSelect.ExecuteReader();
        //        while (reader.Read())
        //        {
        //            types.Add((string)reader["DBMSName"], (Guid)reader["RowID"]);
        //        }
        //    }
        //    return types;
        //}

        //public static Dictionary<string, string> DBTypeName2CLRTypeName()
        //{
        //    Dictionary<string, string> types = new Dictionary<string, string>();
        //    using (SqlConnection con = new SqlConnection(DAL.connectionString))
        //    {
        //        con.Open();
        //        SqlCommand cmdSelect = new SqlCommand("SELECT * FROM [Types]", con);
        //        SqlDataReader reader = cmdSelect.ExecuteReader();
        //        while (reader.Read())
        //        {
        //            types.Add((string)reader["DBMSName"], (string)reader["CLRName"]);
        //        }
        //    }
        //    return types;
        //}

        //public static Dictionary<string, string> CLRTypeName2DBTypeName()
        //{
        //    Dictionary<string, string> types = new Dictionary<string, string>();
        //    using (SqlConnection con = new SqlConnection(DAL.connectionString))
        //    {
        //        con.Open();
        //        SqlCommand cmdSelect = new SqlCommand("SELECT * FROM [Types]", con);
        //        SqlDataReader reader = cmdSelect.ExecuteReader();
        //        while (reader.Read())
        //        {
        //            types.Add((string)reader["CLRName"], (string)reader["DBMSName"]);
        //        }
        //    }
        //    return types;
        //}

        public static string CLRTypeName2DBTypeName(string clrType)
        {
            string dbmsTypeName = String.Empty;
            using (SqlConnection con = new SqlConnection(DAL.connectionString))
            {
                con.Open();
                SqlCommand cmdSelectDBMSTypeName = new SqlCommand(String.Format(@"select [DBMSName] from [dbo].[Types] where [CLRName] = '{0}'", clrType), con);
                dbmsTypeName = (string)cmdSelectDBMSTypeName.ExecuteScalar();
            }
            return dbmsTypeName;
        }

        public static string DBTypeName2CLRTypeName(string dbType)
        {
            string clrTypeName = String.Empty;
            using (SqlConnection con = new SqlConnection(DAL.connectionString))
            {
                con.Open();
                SqlCommand cmdSelectDBMSTypeName = new SqlCommand(String.Format(@"select [CLRName] from [dbo].[Types] where [DBMSName] = '{0}'", dbType), con);
                clrTypeName = (string)cmdSelectDBMSTypeName.ExecuteScalar();
            }
            return clrTypeName;
        }

        public static Type DBTypeName2CLRType(string dbType, bool isNullable)
        {
            Type clrType;
            string clrTypeName;
            using (SqlConnection con = new SqlConnection(DAL.connectionString))
            {
                con.Open();
                SqlCommand cmdSelectDBMSTypeName = new SqlCommand(String.Format(@"select [CLRName] from [dbo].[Types] where [DBMSName] = '{0}'", dbType), con);
                clrTypeName = (string)cmdSelectDBMSTypeName.ExecuteScalar();
            }

            if (isNullable == false || String.Compare(clrTypeName, "String", false) == 0)
                clrType = Type.GetType("System." + clrTypeName);
            else
                //clrType = Type.GetType("System.Nullable").MakeGenericType(Type.GetType("System." + clrTypeName));
                clrType = (typeof(System.Nullable<>)).MakeGenericType(Type.GetType("System." + clrTypeName));

            return clrType;
        }

        public static bool IsCLRTypeNullable(Type type)
        {
            return (type.Name == typeof(Nullable<>).Name || type == typeof(string));
        }
    }
}
